/**
 * Copyright(c) 2025 hbswxx.com, All Rights Reserved. Author: kouqiang Create date: 2025/5/30
 */
package cn.sw.syncdata.job.heart;

import cn.sw.syncdata.entity.HeartInfo;
import cn.sw.syncdata.util.Util;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

/**
 * description: 生成规则，创建数据
 *
 * @author kouqiang
 * @date 2025/5/30 09:02
 */
@Slf4j
@RequiredArgsConstructor
public class ThreadCreateHeart implements Runnable {
    private final List<HeartInfo> heartInfoList;
    private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    // private final ILocalDataBaseHeaderMapper localDataBaseHeaderMapper;
    private final String sdate;
    private final String edate;
    private final int total;
    private final String filename;

    //    public ThreadCreateHeart(List<HeartInfo> heartInfoList, String sdate, String edate, int total, ILocalDataBaseHeaderMapper localDataBaseHeaderMapper) {
//        this.heartInfoList = heartInfoList;
//        this.sdate = sdate;
//        this.edate = edate;
//        this.total = total;
//        this.localDataBaseHeaderMapper = localDataBaseHeaderMapper;
//    }
    @Override
    public void run() {
        Random random = new Random();
        LocalDateTime sDateTime = LocalDateTime.parse(sdate + "T00:00:00", formatter);
        LocalDateTime eDateTime = LocalDateTime.parse(edate + "T23:59:59", formatter);
        StringBuffer sb = new StringBuffer();
        HashSet<String> hashSet = new HashSet<>();
        int dataSize = heartInfoList.size();
        sb.append("community_indexcode, log_level, log_info, addtime");
        sb.append("\n");
        for (int i = 0; i < total; i++) {
            LocalDateTime newDateTime = Util.randomBetween(sDateTime, eDateTime);
            HeartInfo newInfo1 = heartInfoList.get(random.nextInt(dataSize));
            HeartInfo newInfo2 = heartInfoList.get(random.nextInt(dataSize));
            newInfo1.setAddtime(newDateTime);
            newInfo2.setAddtime(newDateTime);
            hashSet.add(newInfo1.toString());
            hashSet.add(newInfo2.toString());
            if (hashSet.size() > 2000) {
                for (String line : hashSet) {
                    sb.append(line);
                    sb.append("\n");
                }
                wrteList(sb.toString());
                sb.setLength(0);
                hashSet.clear();
            }
        }
        if (sb.length() > 0) {
            wrteList(sb.toString());
        }
    }

    private void wrteList(String lines) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) {
            writer.write(lines);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    @Override
//    public void run() {
//       HashSet<HeartInfo> hashSet = new HashSet<>();
//       // HashMap<String, Integer> hashMap = new HashMap<>();
//     //   List<HeartInfo> newHeartInfoList = new ArrayList<>();
//        LocalDateTime sDateTime = LocalDateTime.parse(sdate + "T00:00:00", formatter);
//        LocalDateTime eDateTime = LocalDateTime.parse(edate + "T23:59:59", formatter);
//        Random random = new Random();
//        int dataSize = heartInfoList.size();
//        int datanum = 0;
////        HeartInfo newInfo11 = heartInfoList.get(2);
////        HeartInfo newInfo21 = heartInfoList.get(2);
////        hashSet.add(newInfo11);
////        hashSet.add(newInfo21);
////        HeartInfo newInfo33 = new HeartInfo(newInfo11);
////        newInfo33.setAddtime(newInfo11.getAddtime());
////        hashSet.add(newInfo33);
//        while (datanum < total) {
//            LocalDateTime newDateTime = Util.randomBetween(sDateTime, eDateTime);
//            HeartInfo newInfo1 = heartInfoList.get(random.nextInt(dataSize));
//            HeartInfo newInfo2 = heartInfoList.get(random.nextInt(dataSize));
//            newInfo1.setAddtime(newDateTime);
//            newInfo2.setAddtime(newDateTime);
////            if (!hashMap.containsKey(newInfo1.createString())) {
////                hashMap.put(newInfo1.createString(), 1);
////                newHeartInfoList.add(newInfo1);
////            } else {
////                hashMap.put(newInfo1.createString(), hashMap.get(newInfo1.createString()) + 1);
////            }
////            if(!hashSet.contains(newInfo2.createString())){
////                hashSet.add(newInfo2.createString());
////                newHeartInfoList.add(newInfo2);
////            }
////            hashSet.add(newInfo1);
////            hashSet.add(newInfo2);
////            HeartInfo newInfo3 = new HeartInfo(newInfo1);
////            HeartInfo newInfo4 = new HeartInfo(newInfo2);
////            newInfo3.setCommunityIndexcode(newInfo1.getCommunityIndexcode());
////            newInfo4.setCommunityIndexcode(newInfo2.getCommunityIndexcode());
////            newDateTime = Util.randomBetween(sDateTime, eDateTime);
////            newInfo3.setAddtime(newDateTime);
////            newInfo4.setAddtime(newDateTime);
////            if(!hashSet.contains(newInfo3.createString())){
////                hashSet.add(newInfo3.createString());
////                newHeartInfoList.add(newInfo3);
////            }
////            if(!hashSet.contains(newInfo4.createString())){
////                hashSet.add(newInfo4.createString());
////                newHeartInfoList.add(newInfo4);
////            }
//           /* if (!hashMap.containsKey(newInfo2.createString())) {
//                hashMap.put(newInfo2.createString(), 1);
//                newHeartInfoList.add(newInfo2);
//            } else {
//                hashMap.put(newInfo2.createString(), hashMap.get(newInfo2.createString()) + 1);
//            }
//            if (!hashMap.containsKey(newInfo3.createString())) {
//                hashMap.put(newInfo3.createString(), 1);
//                newHeartInfoList.add(newInfo3);
//            } else {
//                hashMap.put(newInfo3.createString(), hashMap.get(newInfo3.createString()) + 1);
//            }
//            if (!hashMap.containsKey(newInfo4.createString())) {
//                hashMap.put(newInfo4.createString(), 1);
//                newHeartInfoList.add(newInfo4);
//            } else {
//                hashMap.put(newInfo4.createString(), hashMap.get(newInfo4.createString()) + 1);
//            }*/
//            hashSet.add(newInfo1);
//            hashSet.add(newInfo2);
////            hashSet.add(newInfo3);
////            hashSet.add(newInfo4);
//
//
////            hashSet.add(newInfo3);
////            hashSet.add(newInfo4);
//            if (hashSet.size() > 2000) {
//                List<HeartInfo> newHeartInfoList = new ArrayList<>(hashSet);
////                log.info("json111" + JSONObject.toJSONString(hashMap));
////                log.info("json:" + JSONObject.toJSONString(newHeartInfoList));
//                long resultTotal=0;
//                try {
//                    resultTotal = localDataBaseHeaderMapper.insertDatas(newHeartInfoList);
//                }catch (Exception ex)
//                {
//                    log.error("error"+ex);
//                }
//                datanum += resultTotal;
//                log.info("录入了{}条数据，合计{}条", resultTotal, datanum);
//                newHeartInfoList.clear();
//                // hashSet.clear();
//            }
//        }
//        //  List<HeartInfo> newHeartInfoList = new ArrayList<>(hashSet);
//        if (hashSet.size() > 0) {
//            List<HeartInfo> newHeartInfoList = new ArrayList<>(hashSet);
//            long resultTotal = localDataBaseHeaderMapper.insertDatas(newHeartInfoList);
//            datanum += resultTotal;
//        }
//        log.info("完成入库：" + datanum);
//
//    }
}
